<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="108px"
    >
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="预约用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入预约用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="内容编号" prop="contentId">
        <el-input
          v-model="queryParams.contentId"
          placeholder="请输入内容编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="定金金额" prop="deposit">
        <el-input
          v-model="queryParams.deposit"
          placeholder="请输入定金金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总金额" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="预约日期" prop="appointmentDate">
        <el-date-picker
          clearable
          v-model="queryParams.appointmentDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预约日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预约时间段" prop="timeSlot">
        <el-input
          v-model="queryParams.timeSlot"
          placeholder="请输入预约时间段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="预约姓名" prop="receiveName">
        <el-input
          v-model="queryParams.receiveName"
          placeholder="请输入预约姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约地址" prop="receiveAddress">
        <el-input
          v-model="queryParams.receiveAddress"
          placeholder="请输入预约地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约电话" prop="receivePhonenum">
        <el-input
          v-model="queryParams.receivePhonenum"
          placeholder="请输入预约电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="预约地址经度" prop="receiveLongitude">
        <el-input
          v-model="queryParams.receiveLongitude"
          placeholder="请输入预约地址经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约地址纬度" prop="receiveLatitude">
        <el-input
          v-model="queryParams.receiveLatitude"
          placeholder="请输入预约地址纬度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:appointment:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:appointment:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:appointment:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:appointment:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="appointmentList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <el-table-column label="订单号" align="center" prop="orderId" />
      <!-- <el-table-column label="预约用户id" align="center" prop="userId" /> -->
      <!-- <el-table-column label="内容编号" align="center" prop="contentId" /> -->
      <el-table-column label="预约内容" align="center" prop="content" />

      <!-- <el-table-column label="定金金额" align="center" prop="deposit" />
      <el-table-column label="总金额" align="center" prop="totalAmount" /> -->
      <el-table-column
        label="预约日期"
        align="center"
        prop="appointmentDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.appointmentDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约时间段" align="center" prop="timeSlot" />
      <el-table-column label="预约姓名" align="center" prop="receiveName" />
      <el-table-column label="预约地址" align="center" prop="receiveAddress" />
      <el-table-column
        label="预约电话"
        align="center"
        prop="receivePhonenum"
      />
      <!-- <el-table-column
        label="预约地址经度"
        align="center"
        prop="receiveLongitude"
      />
      <el-table-column
        label="预约地址纬度"
        align="center"
        prop="receiveLatitude"
      /> -->
      <!-- <el-table-column label="用户需求描述" align="center" prop="demand" /> -->

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div>{{ type[scope.row.status] }}</div>
        </template>
      </el-table-column>

      <!-- <el-table-column label="图片描述" align="center" prop="demandImg" /> -->

      <!-- <el-table-column
        label="图片描述"
        align="center"
        prop="demandImg"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.demandImg" :width="50" :height="50" />
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remark" /> -->

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:appointment:edit']"
            >修改</el-button
          > -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:appointment:remove']"
            >删除</el-button
          > -->

          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status == 0"
            @click="voided(scope.row, 2)"
            v-hasPermi="['system:appointment:remove']"
            >跟进</el-button
          >

          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status == 2"
            @click="voided(scope.row, 4)"
            v-hasPermi="['system:appointment:remove']"
            >完成</el-button
          >

          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status != 5 && scope.row.status != 4"
            @click="voided(scope.row, 5)"
            v-hasPermi="['system:appointment:remove']"
            >作废</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务预约对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="订单号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单号  FX开头" />
        </el-form-item>
        <el-form-item label="预约用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入预约用户id" />
        </el-form-item>
        <el-form-item label="内容编号" prop="contentId">
          <el-input v-model="form.contentId" placeholder="请输入内容编号" />
        </el-form-item>
        <el-form-item label="预约内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="定金金额" prop="deposit">
          <el-input v-model="form.deposit" placeholder="请输入定金金额" />
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="预约日期" prop="appointmentDate">
          <el-date-picker
            clearable
            v-model="form.appointmentDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预约日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预约时间段" prop="timeSlot">
          <el-input v-model="form.timeSlot" placeholder="请输入预约时间段" />
        </el-form-item>
        <el-form-item label="预约姓名" prop="receiveName">
          <el-input v-model="form.receiveName" placeholder="请输入预约姓名" />
        </el-form-item>
        <el-form-item label="预约地址" prop="receiveAddress">
          <el-input
            v-model="form.receiveAddress"
            placeholder="请输入预约地址"
          />
        </el-form-item>
        <el-form-item label="预约电话" prop="receivePhonenum">
          <el-input
            v-model="form.receivePhonenum"
            placeholder="请输入预约电话"
          />
        </el-form-item>
        <el-form-item label="预约地址经度" prop="receiveLongitude">
          <el-input
            v-model="form.receiveLongitude"
            placeholder="请输入预约地址经度"
          />
        </el-form-item>
        <el-form-item label="预约地址纬度" prop="receiveLatitude">
          <el-input
            v-model="form.receiveLatitude"
            placeholder="请输入预约地址纬度"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="用户需求描述" prop="demand">
          <el-input
            v-model="form.demand"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <!-- <el-form-item label="图片描述" prop="demandImg">
          <el-input
            v-model="form.demandImg"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAppointment,
  getAppointment,
  delAppointment,
  addAppointment,
  updateAppointment,
} from "@/api/fx/category/appointment.js";

export default {
  name: "Appointment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务预约表格数据
      appointmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        userId: null,
        contentId: null,
        content: null,
        status: null,
        deposit: null,
        totalAmount: null,
        appointmentDate: null,
        timeSlot: null,
        receiveName: null,
        receiveAddress: null,
        receivePhonenum: null,
        receiveLongitude: null,
        receiveLatitude: null,
        demand: null,
        demandImg: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      type: ["待派单", "待上门", "进行中", "待支付", "已完成", "已作废"],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询服务预约列表 */
    getList() {
      this.loading = true;
      listAppointment(this.queryParams).then((response) => {
        this.appointmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    voided(row, type) {
      this.form = row;
      this.form.status = type;
      updateAppointment(this.form).then((response) => {
        this.getList();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        userId: null,
        contentId: null,
        content: null,
        status: null,
        deposit: null,
        totalAmount: null,
        appointmentDate: null,
        timeSlot: null,
        receiveName: null,
        receiveAddress: null,
        receivePhonenum: null,
        receiveLongitude: null,
        receiveLatitude: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        demand: null,
        demandImg: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务预约";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAppointment(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务预约";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAppointment(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppointment(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除服务预约编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAppointment(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/appointment/export",
        {
          ...this.queryParams,
        },
        `appointment_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
